package org.dfzt.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.dfzt.common.util.SpringContextUtils;
//import org.dfzt.modules.online.cgform.converter.b;
import org.dfzt.modules.online.cgform.entity.OnlCgformField;
import org.dfzt.modules.online.cgform.entity.OnlCgformHead;
import org.dfzt.modules.online.cgform.mapper.OnlCgformFieldMapper;
import org.dfzt.modules.online.cgform.service.IOnlCgformHeadService;
import org.dfzt.modules.online.cgform.service.IOnlCgformSqlService;
import org.dfzt.modules.online.cgform.util.b;
import org.dfzt.modules.online.config.exception.BusinessException;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("onlCgformSqlServiceImpl")
public class OnlCgformSqlServiceImpl
        implements IOnlCgformSqlService {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;

    @Override
    public void saveBatchOnlineTable(OnlCgformHead head, List<OnlCgformField> fieldList, List<Map<String, Object>> dataList) {
      SqlSession sqlSession = null;
        try {
            org.dfzt.modules.online.cgform.converter.b.a(2, dataList, fieldList);

            sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);

            OnlCgformFieldMapper onlCgformFieldMapper = (OnlCgformFieldMapper) sqlSession.getMapper(OnlCgformFieldMapper.class);

            short s = 1000;

            if (s >= dataList.size()) {
                for (byte b = 0; b < dataList.size(); b++) {
                    String str = JSON.toJSONString(dataList.get(b));
                    a(str, head, fieldList, onlCgformFieldMapper);
                }
            } else {
                for (short s1 = 0; s1 < dataList.size(); s1++) {
                    String str = JSON.toJSONString(dataList.get(s1));
                    a(str, head, fieldList, onlCgformFieldMapper);
                    if (s1 % s == '\000') {
                        sqlSession.commit();

                        sqlSession.clearCache();
                    }
                }
            }

            sqlSession.commit();
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {

            sqlSession.close();
        }
    }


    @Override
    public void saveOrUpdateSubData(String subDataJsonStr, OnlCgformHead head, List<OnlCgformField> subFiledList) throws BusinessException {
        OnlCgformFieldMapper onlCgformFieldMapper = (OnlCgformFieldMapper) SpringContextUtils.getBean(OnlCgformFieldMapper.class);
        a(subDataJsonStr, head, subFiledList, onlCgformFieldMapper);
    }


    private void a(String paramString, OnlCgformHead paramOnlCgformHead, List<OnlCgformField> paramList, OnlCgformFieldMapper paramOnlCgformFieldMapper) throws BusinessException {
        JSONObject jSONObject = JSONObject.parseObject(paramString);
        int i = this.onlCgformHeadService.executeEnhanceJava("import", "start", paramOnlCgformHead, jSONObject);
        String str = paramOnlCgformHead.getTableName();
        if (1 == i) {

            Map map = org.dfzt.modules.online.cgform.util.b.a(str, paramList, jSONObject);
            paramOnlCgformFieldMapper.executeInsertSQL(map);
        } else if (2 == i) {

            Map map = org.dfzt.modules.online.cgform.util.b.b(str, paramList, jSONObject);
            paramOnlCgformFieldMapper.executeUpdatetSQL(map);
        } else if (0 == i) {

        }
    }
}
